package com.aysf.drafter.controller;

import com.aysf.drafter.entity.BedLayout;
import com.aysf.drafter.entity.CustomerCheckIn;
import com.aysf.drafter.entity.Result;
import com.aysf.drafter.service.IBedLayoutService;
import com.aysf.drafter.service.ICustomerCheckInService;
import com.aysf.drafter.vo.BedVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author MaoZz
 * @Date 2024/12/21 16:47
 */
@CrossOrigin
@RestController
@RequestMapping("/Init")
public class InitController {
    @Autowired
    private IBedLayoutService bedLayoutService;
    @Autowired
    private ICustomerCheckInService customerCheckInService;

    @RequestMapping("/getAvail")
    public Result getAvail() {

        return bedLayoutService.get();

    }


    @RequestMapping("/getdata")
    public Result getdata(Integer res[]) {
        ArrayList<BedVO> customerCheckIns = new ArrayList<>();
        for (int i = 0; i < res.length; i++) {
//            List<BedLayout> list = bedLayoutService
//                    .list(new QueryWrapper<BedLayout>().eq("room_number", res[i]));
            BedLayout bedLayout = bedLayoutService.getOne(new QueryWrapper<BedLayout>()
                    .eq("room_number", res[i]));
            BedVO bedVO = new BedVO();
            bedVO.setRoomNumber(String.valueOf(res[i]));
            bedVO.setCustomerId(bedLayout.getCustomerId());
            CustomerCheckIn customerCheckIn =
                    customerCheckInService.getById(bedLayout.getCustomerId());
            if(customerCheckIn != null) {
                bedVO.setName(customerCheckIn.getName());
                bedVO.setPhone(customerCheckIn.getPhone());
                bedVO.setGender(customerCheckIn.getGender());
                bedVO.setIdCard(customerCheckIn.getIdCard());
                bedVO.setCheckInDate(customerCheckIn.getCheckInDate());
            }
            bedVO.setBuilding(bedLayout.getBuilding());
            bedVO.setFloor(bedLayout.getFloor());
            bedVO.setBedStatus(bedLayout.getBedStatus());
            customerCheckIns.add(bedVO);
        }
        if (customerCheckIns.size() > 0) {
            return new Result("success", customerCheckIns);
        } else return new Result("error", null);


    }


}
